#include "GameObject.h"

#include "Component.h"
#include "ConsoleColor.h"
#include <iostream>
#include <algorithm>

GameObject::GameObject(const std::string &name, const std::string &description, const std::string &indefinite_article) 
	: name(name), indefinite_article(indefinite_article), description(description), weight(UINT32_MAX)
{
}

GameObject::~GameObject()
{
}

bool GameObject::is(const std::string &name)
{
    if(this->name.find(name) != std::string::npos)
        return true;
    else
        return false;
}

void GameObject::look() 
{ 
	std::cout << "A " << yellow << name << white << " is here." << std::endl;
}

void GameObject::examine() 
{ 
	std::cout << yellow << description << white << std::endl;
}

void GameObject::update(unsigned int tick)
{
	std::for_each(begin(components), end(components), [&](const ComponentPtr &component)
	{
		component->update(tick);
	});
}

void GameObject::add(ComponentPtr component)
{
	components.push_back(component);
}

void GameObject::remove(const ComponentPtr &component)
{
	for(unsigned int i = 0; i < components.size(); i++)
	{
		if(components[i]->getName() == component->getName())
		{
			components[i] = components.back();
			components.pop_back();
			return;
		}
	}
}

bool GameObject::hasComponent(const std::string &name) const
{
	for(unsigned int i = 0; i < components.size(); i++)
	{
		if(components[i]->getName() == name)
			return true;
	}
	return false;
}

bool GameObject::canHandleCommand(const CommandPtr &command)
{
	for(unsigned int i = 0; i < components.size(); i++)
	{
		if(components[i]->hasCommand(command))
			return true;
	}
	return false;
}
